import {
  api,storage
} from "../util";
import authorize from './authorize.js'
import {
  account
} from "./gloab";
const app = getApp();
var upload = {
  upload_image: (count, callback) => {
    var that = this,
      image_list = [];
    wx.chooseImage({
      count: count || 1, //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths; 
        //启动上传等待中...
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        var uploadImgCount = 0;
        for (var i = 0, h = tempFilePaths.length; i < h; i++) {
          wx.uploadFile({
            url: api.host + '/ufs/upload',
            filePath: tempFilePaths[i],
            name: 'uploadfile_ant',
            formData: { 
              ProprietorId: app.dns.proprietorId,
              fkflag: app.dns.proprietorId,
              fkId: app.dns.proprietor
            },
            header: {
              "Content-Type": "multipart/form-data"
            },
            success: function(res) {
              uploadImgCount++;
              if (res.statusCode == 200) {
                image_list.push("/" + res.data);
              }
              //如果是最后一张,则隐藏等待中
              if (uploadImgCount == tempFilePaths.length) {
                wx.hideToast();
                if (callback != undefined && typeof(callback) == "function") {
                  callback(image_list);
                }
              }
            },
            fail: function(res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function(res) {}
              })
            }
          });
        }
      }
    });

  },
  upload_image_server: function (data, callback) {
    storage.put("imgNum",data.num)
    //启动上传等待中...
    wx.showToast({
      title: '正在上传...',
      icon: 'loading',
      mask: true,
      duration: 10000
    });
    var image_list = [], uploadImgCount = 0,tempFilePaths=data.tempFilePaths;
    account.get_upload_token(data,app.dns.proprietorId,(upload_token)=>{
       this.upload_token=upload_token;
       this.uploadOneByOne(tempFilePaths, 0, 0, 0,image_list,callback)
    });


  },
  /**
  * 采用递归的方式上传多张
  */
  uploadOneByOne(imgPaths,successUp, failUp, count,uploadlist,callback) {
    var imgNum=storage.get("imgNum")
    // console.log(count,'====count',imgNum);
    var that = this;
    wx.showLoading({
      title: '正在上传第' + (count + 1) + '个',
    })
    wx.uploadFile({
      url: api.host+"/ufs/upload",
      filePath: imgPaths[count],
      name: 'uploadfile_ant'+count,
      header:{
        token:this.upload_token,
        module:imgNum
      },
      // formData: {
      //   method: 'POST',   //请求方式
      //   ProprietorId: app.dns.proprietorId,
      //   fkflag: app.dns.proprietorId,
      //   fkId: app.dns.proprietor
      // },
      success: function (res) {
        if (res.statusCode == 200) {
          successUp++;//成功+1
          let content=JSON.parse(res.data);
          // console.log(Array.isArray(res.data),'----data', res.data);
          // console.log(content,'----====');
          // uploadlist.push("/" + content.path);
          if(content.success){
            uploadlist.push("/" + content.path);
            // console.log(uploadlist,'------1');
          }else{
            failUp++;//失败+1
          }
        }
        else{
          failUp++;//失败+1
        }
      },

      fail: function (e) {
        failUp++;//失败+1
      },
      complete: function (e) {
        count++;//下一张
        if (count == imgPaths.length) {
          //上传完毕，作一下提示
          wx.showToast({
            title: '上传成功',
            icon: 'success',
            duration: 2000
          });
          if (callback != undefined && typeof (callback) == "function") {
            callback(uploadlist);
            // console.log(uploadlist,'-------imgs');
          }  
        } else {
          //递归调用，上传下一张
          that.uploadOneByOne(imgPaths, successUp, failUp, count, uploadlist, callback);
          // console.log('333333');
        }
      }
    })
  },
  signPhoto(imgPaths,callback){
    // wx.showLoading({ title: '正在上传...' })
    wx.uploadFile({
      url: api.host + '/upload.axd',
      filePath: imgPaths,
      name: 'uploadfile_ant',
      formData: { 
        ProprietorId: app.dns.proprietorId,
        fkflag: app.dns.proprietorId,
        fkId: app.dns.proprietor
      },
      header: {
        "Content-Type": "multipart/form-data"
      },
      success: function(res) {
        wx.hideToast();
        console.log(res,'=======res');
        callback(res)
      },
      fail: function(res) {
        wx.hideToast();
        wx.showModal({
          title: '错误提示',
          content: '上传图片失败',
          showCancel: false,
          success: function(res) {}
        })
      }
    });
  },
}
export {
  upload
}